﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace KD.Sms.Monitor
{
    public partial class frmRunFault : Form
    {
        private DateTime _dtMark;
        private bool _bRun = false;
        private System.Threading.Thread m_Run;
        bool _bIsBusyCyj = false;
        bool _bIsBusyFzj = false;
        public frmRunFault()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 检查
        /// </summary>
        private void DoCheck()
        {
            while (_bRun)
            {
                if (_dtMark < DateTime.Now.AddSeconds(-20))
                {
                    DoClear();
                }
                System.Threading.Thread.Sleep(5000);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmRunFault_Load(object sender, EventArgs e)
        {
            Task.Cyj.Instance.PlcDataReadOut += OnPlcDataReadOutCyj;
            Task.Fzj.Instance.PlcDataReadOut += OnPlcDataReadOutFzj;

            _bRun = true;
            m_Run = new System.Threading.Thread(new System.Threading.ThreadStart(DoCheck));
            m_Run.IsBackground = true;
            m_Run.Start();
        }


        /// <summary>
        /// 清理
        /// </summary>
        private void DoClear()
        {
            UI.FormHelper.ApplicationClear(ricInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        private void OnPlcDataReadOutFzj(object send, Task.ActiveReadEventArgs e)
        {
            if (_bIsBusyFzj)
                return;
            try
            {
                _bIsBusyFzj = true;
                short[] _arrReads = (short[])e.E_ReadValue;
                int[] _aryBits40 = DataConvert.Ushort2Binary((int)(_arrReads[40 / 2]));
                int[] _aryBits42 = DataConvert.Ushort2Binary((int)(_arrReads[42 / 2]));
                int[] _aryBits46 = DataConvert.Ushort2Binary((int)(_arrReads[46 / 2]));
                int[] _aryBits48 = DataConvert.Ushort2Binary((int)(_arrReads[48 / 2]));
                int[] _aryBits50 = DataConvert.Ushort2Binary((int)(_arrReads[50 / 2]));
                int[] _aryBits52 = DataConvert.Ushort2Binary((int)(_arrReads[52 / 2]));//控制电源提示信息


                if (_aryBits40[0] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:面板急停未复位");
                if (_aryBits40[1] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:电源相序出错");
                if (_aryBits40[2] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:直流24V电源检测无");
                if (_aryBits40[3] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:称重斗未退到位不能开盖");
                if (_aryBits40[4] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:称重斗未退到位不能关盖");
                if (_aryBits40[5] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:集样器正运行不能开盖");
                if (_aryBits40[6] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:集样器正运行不能关盖");
                if (_aryBits40[8] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:集样器正运行不能放料");
                if (_aryBits40[9] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:集样器正运行不能退回");
                if (_aryBits40[11] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:出桶处检测有异物");
                if (_aryBits40[12] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:样桶盖未开到位不能放料");
                if (_aryBits40[13] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:桶盖未开到位不能转桶");
                if (_aryBits40[14] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:桶盖检测信号有不能转桶");
                if (_aryBits40[15] == 1)
                    DoShowFault(UI.MsgType.Waring, "封装机:电磁铁正在运行不能转桶");

                if (_aryBits42[0] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:集样桶盖开关限位同时接通");
                if (_aryBits42[1] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:称重料斗开关限位同时接通");
                if (_aryBits42[2] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:推桶机构开关限位同时接通");
                if (_aryBits42[3] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:集样器计数故障");
                if (_aryBits42[4] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:集样器原位故障");
                if (_aryBits42[8] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:料斗前进4S后限位仍接通");
                if (_aryBits42[9] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:料斗后退4S前限位仍接通");
                if (_aryBits42[10] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:开盖上升4S下限位接通");
                if (_aryBits42[11] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:开盖下降4S上限位接通");
                if (_aryBits42[12] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:出桶4S进桶位限位扔接通");
                if (_aryBits42[13] == 1)
                    DoShowFault(UI.MsgType.Error, "封装机:进桶4S出桶位限位扔接通");

                if (_aryBits46[0] == 1)
                    DoShowFault(UI.MsgType.Error, "自动复位称重斗超时");
                if (_aryBits46[1] == 1)
                    DoShowFault(UI.MsgType.Error, "自动复位开关盖机构超时");
                if (_aryBits46[2] == 1)
                    DoShowFault(UI.MsgType.Error, "自动复位集样器旋转超时");

                if (_aryBits48[0] == 1)
                    DoShowFault(UI.MsgType.Error, "集样桶盖电机动作超时");
                if (_aryBits48[1] == 1)
                    DoShowFault(UI.MsgType.Error, "称重料斗电机动作超时");
                if (_aryBits48[2] == 1)
                    DoShowFault(UI.MsgType.Error, "推桶机构电机动作超时");
                if (_aryBits48[3] == 1)
                    DoShowFault(UI.MsgType.Error, "集样器自动转桶超时");
                if (_aryBits48[6] == 1)
                    DoShowFault(UI.MsgType.Error, "异常开盖次数超限");

                if (_aryBits50[0] == 1)
                    DoShowFault(UI.MsgType.Error, "集样器断路器跳闸故障FR1");
                if (_aryBits50[1] == 1)
                    DoShowFault(UI.MsgType.Error, "称重料斗路器跳闸故障FR3");
                if (_aryBits50[2] == 1)
                    DoShowFault(UI.MsgType.Error, "集样桶盖路器跳闸故障FR4");
                if (_aryBits50[3] == 1)
                    DoShowFault(UI.MsgType.Error, "进出桶推桶电机跳闸故障RF6");
                if (_aryBits50[4] == 1)
                    DoShowFault(UI.MsgType.Error, "样桶输送断路器跳闸故障FR5");
                if (_aryBits50[5] == 1)
                    DoShowFault(UI.MsgType.Error, "除尘风机断路器跳闸故障FR2");
                
            }
            catch (Exception ex)
            {
                FormHelper.ApplicationShow(UI.MsgType.Error, "故障解析出错!详见日志!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "故障解析出错!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
            }
            finally
            {
                _bIsBusyFzj = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        private void OnPlcDataReadOutCyj(object send, Task.ActiveReadEventArgs e)
        {
            if (_bIsBusyCyj)
                return;
            try
            {
                _bIsBusyCyj = true;
                short[] _arrReads = (short[])e.E_ReadValue;
                int[] _aryBits370 = DataConvert.Ushort2Binary((int)(_arrReads[70 / 2]));
                int[] _aryBits376 = DataConvert.Ushort2Binary((int)(_arrReads[76 / 2]));
                int[] _aryBits378 = DataConvert.Ushort2Binary((int)(_arrReads[78 / 2]));
                int[] _aryBits380 = DataConvert.Ushort2Binary((int)(_arrReads[80 / 2]));
                int[] _aryBits382 = DataConvert.Ushort2Binary((int)(_arrReads[82 / 2]));
                int[] _aryBits384 = DataConvert.Ushort2Binary((int)(_arrReads[84 / 2]));
                int[] _aryBits352 = DataConvert.Ushort2Binary((int)(_arrReads[52 / 2]));

                if (_arrReads[(402 - 300) / 2] == (short)1)
                    DoShowFault(UI.MsgType.Waring, "采样完成，等待上一车封装完成后卸料");

                if (_aryBits370[0] == 1)
                    DoShowFault(UI.MsgType.Waring, "大车已到前限位,不能前进");
                if (_aryBits370[1] == 1)
                    DoShowFault(UI.MsgType.Waring, "大车已到后限位,不能后退");
                if (_aryBits370[2] == 1)
                    DoShowFault(UI.MsgType.Waring, "小车已到左限位,不能左行");
                if (_aryBits370[3] == 1)
                    DoShowFault(UI.MsgType.Waring, "小车已到右限位,不能右行");
                if (_aryBits370[4] == 1)
                    DoShowFault(UI.MsgType.Waring, "采样头已到上限位,不能上升");
                if (_aryBits370[5] == 1)
                    DoShowFault(UI.MsgType.Waring, "采样头已到下限位,不能下降");
                if (_aryBits370[6] == 1)
                    DoShowFault(UI.MsgType.Waring, "采样头未到上限位,大小车不能动");
                if (_aryBits370[7] == 1)
                    DoShowFault(UI.MsgType.Waring, "采样不在允许下降区，不能移动大车");
                if (_aryBits370[8] == 1)
                    DoShowFault(UI.MsgType.Waring, "采样头不在上限位，大车不能行走");
                if (_aryBits370[9] == 1)
                    DoShowFault(UI.MsgType.Waring, "集料斗门没关到位，小车不能行走");
                if (_aryBits370[10] == 1)
                    DoShowFault(UI.MsgType.Waring, "集料斗没有关到位，不能升降");
                //if (_aryBits370[12] == 1)
                //    DoShowFault(UI.MsgType.Waring, "集样器门没关到位，不能转桶");

                if (_aryBits376[0] == 1)
                    DoShowFault(UI.MsgType.Error, "大车故障，检查QF1，QF1.1，FR1,FR2，变频器UF1");
                if (_aryBits376[1] == 1)
                    DoShowFault(UI.MsgType.Error, "小车故障，检查QF2，QF2.1，变频器UF2");
                if (_aryBits376[2] == 1)
                    DoShowFault(UI.MsgType.Error, "升降故障，检查QF3，QF3.1，变频器UF3");
                if (_aryBits376[3] == 1)
                    DoShowFault(UI.MsgType.Error, "旋转故障，检查QF4");
                if (_aryBits376[5] == 1)
                    DoShowFault(UI.MsgType.Error, "集料斗门故障，检查QF5");
                if (_aryBits376[6] == 1)
                    DoShowFault(UI.MsgType.Error, "给料皮带摆动故障，检查QF6");
                if (_aryBits376[7] == 1)
                    DoShowFault(UI.MsgType.Error, "给料皮带故障，检查QF6");

                if (_aryBits376[9] == 1)
                    DoShowFault(UI.MsgType.Error, "刮板故障，检查QF7");
                if (_aryBits376[10] == 1)
                    DoShowFault(UI.MsgType.Error, "破碎故障，检查QF11");
                if (_aryBits376[11] == 1)
                    DoShowFault(UI.MsgType.Error, "缩分皮带故障，检查QF9");
                if (_aryBits376[12] == 1)
                    DoShowFault(UI.MsgType.Error, "缩分器故障，检查QF10");
                if (_aryBits376[13] == 1)
                    DoShowFault(UI.MsgType.Error, "集样器故障，检查QF11");
                if (_aryBits376[14] == 1)
                    DoShowFault(UI.MsgType.Error, "弃料斗门，检查检查QF12");  
                if (_aryBits376[15] == 1)
                    DoShowFault(UI.MsgType.Error, "集样器门故障，检查QF13");

                if (_aryBits378[8] == 1)
                    DoShowFault(UI.MsgType.Error, "相序出错");
                if (_aryBits378[11] == 1)
                    DoShowFault(UI.MsgType.Error, "大车坐标超限");
                if (_aryBits378[12] == 1)
                    DoShowFault(UI.MsgType.Error, "小车坐标超限");
                if (_aryBits378[14] == 1)
                    DoShowFault(UI.MsgType.Error, "升降坐标超限");

                if (_aryBits380[0] == 1)
                    DoShowFault(UI.MsgType.Error, "大车前后限位同时接通");
                if (_aryBits380[1] == 1)
                    DoShowFault(UI.MsgType.Error, "小车左右限位同时接通");
                if (_aryBits380[2] == 1)
                    DoShowFault(UI.MsgType.Error, "升降上下限位同时接通");
                if (_aryBits380[3] == 1)
                    DoShowFault(UI.MsgType.Error, "集料斗开关限位同时接通");
                if (_aryBits380[5] == 1)
                    DoShowFault(UI.MsgType.Error, "弃料斗开关限位同时接通");
                //if (_aryBits380[6] == 1)
                //    DoShowFault(UI.MsgType.Error, "集样器计数故障");

                if (_aryBits382[0] == 1)
                    DoShowFault(UI.MsgType.Error, "防撞检测");
                if (_aryBits382[1] == 1)
                    DoShowFault(UI.MsgType.Error, "大车前进4S后限位仍然接通");
                if (_aryBits382[2] == 1)
                    DoShowFault(UI.MsgType.Error, "大车后退4S前限位仍然接通");
                if (_aryBits382[3] == 1)
                    DoShowFault(UI.MsgType.Error, "大车计数故障");
                if (_aryBits382[4] == 1)
                    DoShowFault(UI.MsgType.Error, "小车左行4S右限位仍然接通");
                if (_aryBits382[5] == 1)
                    DoShowFault(UI.MsgType.Error, "小车右行4S左限位仍然接通");
                if (_aryBits382[6] == 1)
                    DoShowFault(UI.MsgType.Error, "小车计数故障");
                if (_aryBits382[7] == 1)
                    DoShowFault(UI.MsgType.Error, "升降上升4S下限位仍然接通");
                if (_aryBits382[8] == 1)
                    DoShowFault(UI.MsgType.Error, "升降下降4S上限位仍然接通");
                if (_aryBits382[9] == 1)
                    DoShowFault(UI.MsgType.Error, "升降计数故障");

                if (_aryBits384[0] == 1)
                    DoShowFault(UI.MsgType.Error, "集料斗开闭动作超时");
                //if (_aryBits384[2] == 1)
                //    DoShowFault(UI.MsgType.Error, "集样器门开闭动作超时");
                if (_aryBits384[3] == 1)
                    DoShowFault(UI.MsgType.Error, "弃料斗开关动作超时");
                if (_aryBits384[8] == 1)
                    DoShowFault(UI.MsgType.Error, "给料皮带零速故障");
                if (_aryBits384[9] == 1)
                    DoShowFault(UI.MsgType.Error, "缩分皮带零速故障");

                if (_aryBits352[13] == 0)
                    DoShowFault(UI.MsgType.Error, "控制电源未投入");
            }
            catch (Exception ex)
            {
                FormHelper.ApplicationShow(UI.MsgType.Error, "故障解析出错!详见日志!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "故障解析出错!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
            }
            finally
            {
                _bIsBusyCyj = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="msg"></param>
        private void DoShowFault(UI.MsgType type, string msg)
        {
            UI.FormHelper.RicMsgShow(ricInfo, UI.MsgType.Error, msg);
            //插入故障报警
            DAL.SqlLite.Main.Instance.DoInsertFault(msg);
        }

        private void frmRunFault_FormClosing(object sender, FormClosingEventArgs e)
        {
            _bRun = false;
            if (m_Run != null)
            {
                System.Threading.Thread.Sleep(200);
                m_Run.Abort();
            }
        }
    }
}
